package org.tigris.atlas.examples.validation;

import org.tigris.atlas.examples.service.ValidationExampleServiceResponse;
import org.tigris.atlas.examples.service.delegate.EntityMaintenanceServiceDelegate;
import org.tigris.atlas.examples.service.delegate.ServiceDelegateFactory;
import org.tigris.atlas.examples.transfer.TransferObjectFactory;
import org.tigris.atlas.examples.transfer.ValidationExample;
import org.tigris.atlas.examples.transfer.ValidationExampleChild;
import junit.framework.TestCase;

public class TestGetAllErrors extends TestCase {

	public void testGetAllErrorsNoErrors() {
		ValidationExample ex = TransferObjectFactory.createValidationExample();
		ex.setRequiredField("foo");
		
		ValidationExampleChild child = TransferObjectFactory.createValidationExampleChild();
		child.setRequiredField("bar");
		
		ex.addValidationExampleChild(child);
		
		ex = saveValidationExample(ex);
		assertFalse(ex.getMessages().hasErrorMessages());
		assertFalse(child.getMessages().hasErrorMessages());
		assertEquals(0, ex.getAllMessages().getErrorMessageCount());
		assertEquals(0, child.getAllMessages().getErrorMessageCount());
	}

	public void testGetAllErrorsParentErrors() {
		ValidationExample ex = TransferObjectFactory.createValidationExample();
		
		ValidationExampleChild child = TransferObjectFactory.createValidationExampleChild();
		child.setRequiredField("bar");
		
		ex.addValidationExampleChild(child);
		
		ex = saveValidationExample(ex);
		child = (ValidationExampleChild) ex.getValidationExampleChilds().iterator().next();
		assertTrue(ex.getMessages().hasErrorMessages());
		assertFalse(child.getMessages().hasErrorMessages());
		assertTrue(ex.getAllMessages().hasErrorMessages());
		assertTrue(child.getAllMessages().hasErrorMessages());
		assertEquals(1, ex.getMessages().getErrorMessageCount());
		assertEquals(0, child.getMessages().getErrorMessageCount());
		assertEquals(1, ex.getAllMessages().getErrorMessageCount());
		assertEquals(1, child.getAllMessages().getErrorMessageCount());
	}

	public void testGetAllErrorsChildErrors() {
		ValidationExample ex = TransferObjectFactory.createValidationExample();
		ex.setRequiredField("foo");
		
		ValidationExampleChild child = TransferObjectFactory.createValidationExampleChild();
		
		ex.addValidationExampleChild(child);
		
		ex = saveValidationExample(ex);
		child = (ValidationExampleChild) ex.getValidationExampleChilds().iterator().next();
		assertFalse(ex.getMessages().hasErrorMessages());
		assertTrue(ex.getAllMessages().hasErrorMessages());
		assertTrue("Child should have errors", child.getMessages().hasErrorMessages());
		assertTrue("Child should have errors", child.getAllMessages().hasErrorMessages());
		assertEquals(1, ex.getAllMessages().getErrorMessageCount());
		assertEquals(1, child.getAllMessages().getErrorMessageCount());
	}

	public void testGetAllErrorsChildAndParentErrors() {
		ValidationExample ex = TransferObjectFactory.createValidationExample();
		
		ValidationExampleChild child = TransferObjectFactory.createValidationExampleChild();
		
		ex.addValidationExampleChild(child);
		
		ex = saveValidationExample(ex);
		child = (ValidationExampleChild) ex.getValidationExampleChilds().iterator().next();
		assertTrue(ex.getMessages().hasErrorMessages());
		assertTrue("Child should have errors", child.getMessages().hasErrorMessages());
		assertTrue(ex.getAllMessages().hasErrorMessages());
		assertTrue("Child should have errors", child.getAllMessages().hasErrorMessages());
		assertEquals(2, ex.getAllMessages().getErrorMessageCount());
		assertEquals(2, child.getAllMessages().getErrorMessageCount());
		assertEquals(1, ex.getMessages().getErrorMessageCount());
		assertEquals(1, child.getMessages().getErrorMessageCount());
	}

	private ValidationExample saveValidationExample(ValidationExample ex) {
		EntityMaintenanceServiceDelegate delegate = ServiceDelegateFactory.createEntityMaintenanceServiceDelegate();
		ValidationExampleServiceResponse response = delegate.save(ex);
		assertEquals(0, response.getMessages().getErrorMessageCount());
		return response.getValidationExample();
	}
	
}
